package com.code2roc.jarmanage.servlet.filemanage;

import com.code2roc.jarmanage.model.Result;
import com.code2roc.jarmanage.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class SystemFileAddSerlvet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = Result.okResult();
        HashMap paramMap = CommonUtil.getParamMap(req);
        String prefix = ConvertUtil.convert2String(paramMap.get("prefix"));
        if(StringUtil.isEmpty(prefix)){
            result = Result.errorResult().setMsg("根目录禁止新建文件/文件夹");
            resp.getWriter().println(result.toString());
            return;
        }
        String fileName = ConvertUtil.convert2String(paramMap.get("fileName"));
        String type = ConvertUtil.convert2String(paramMap.get("type"));
        String basePath = EnviromentUtil.getFileRootHomePath();
        String filePath = basePath + prefix;
        filePath = filePath + File.separator + fileName;
        if (type.equals("dir")) {
            FileUtil.initfloderPath(filePath);
        } else if (type.equals("file")) {
            File file = new File(filePath);
            file.createNewFile();
        }
        resp.getWriter().println(result.toString());
    }
}
